using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DocLibApp
{
    public partial class FormBrowse : FormBase
    {
        private enum TreeImage
        {
            Owner,
            Location,
            Type,
            Importance,
            Category
        }

        private string[] ListValue = { "DOC_OWNER", "DOC_CAT", "DOC_FORMAT", "DOC_IMPORTANCE", "DOC_LOCATION" };
        private string[] ListName = { "Owner", "Document Category", "Document Type", "Importance", "Location" };

        public FormBrowse()
        {
            InitializeComponent();
            SetupGUI();
        }

        private void SetupGUI()
        {
            //populate filter list
            //SetupCombo(ListValue, ListName, cboFilter);
            PopulateTreeView();

            //setup combo for display
            util.SetupCombo(cboCategory, FormMain.DocLibStaticValueList, "DOC_CATEGORY");
            util.SetupCombo(cboDocType, FormMain.DocLibStaticValueList, "DOC_TYPE");
            util.SetupCombo(cboOwner, FormMain.DocLibStaticValueList, "DOC_OWNER");
            util.SetupCombo(cboImportance, FormMain.DocLibStaticValueList, "DOC_IMPORTANCE");
            util.SetupCombo(cboDocLocation, FormMain.DocLibStaticValueList, "DOC_LOCATION");
        }

        private void PopulateTreeView()
        {
            //1st level - Field List
            for (int i = 0; i < ListValue.Length; i++)
            {
                string value = ListValue[i];
                string display = ListName[i];

                TreeNode newNode = new TreeNode(display);
                newNode.Tag = value;

                int imgIndex = 0;
                switch (value)
                {
                    case "DOC_OWNER":
                        imgIndex = (Int32)TreeImage.Owner;
                        break;
                    case "DOC_CATEGORY":
                        imgIndex = (Int32)TreeImage.Category;
                        break;
                    case "DOC_TYPE":
                        imgIndex = (Int32)TreeImage.Type;
                        break;
                    case "DOC_IMPORTANCE":
                        imgIndex = (Int32)TreeImage.Importance;
                        break;
                    case "DOC_LOCATION":
                        imgIndex = (Int32)TreeImage.Location;
                        break;                   
                }

                newNode.ImageIndex = newNode.SelectedImageIndex = imgIndex;
                tv_DocumentList.Nodes.Add(newNode);


                //populate values for 1st level
                DataRow[] list = FormMain.DocLibStaticValueList.Select("[CODE_GROUP] = '" + value + "'");
                foreach (DataRow dr in list)
                {
                    string desc = dr["DESC"].ToString();
                    string code = dr["CODE"].ToString();

                    TreeNode childNode = new TreeNode(desc);
                    childNode.Tag = code;
                    childNode.ImageIndex = childNode.SelectedImageIndex = imgIndex;

                    newNode.Nodes.Add(childNode);
                }
            }

            tv_DocumentList.ExpandAll();
        }

        private void tv_DocumentList_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (tv_DocumentList.SelectedNode.Level == 1)
            {
                //clear document info display
                ClearValuesFromControls(splitContainer3.Panel2.Controls);

                TreeNode selNode = tv_DocumentList.SelectedNode;
                string value = selNode.Tag.ToString();

                if (selNode.Parent != null && selNode.Parent.Tag != null)
                {
                    string key = selNode.Parent.Tag.ToString().Trim();
                    string field = "";
                    
                    switch (key)
                    {
                        case "DOC_CATEGORY":
                            field = "CATEGORY";
                            break;
                        case "DOC_TYPE":
                            field = "TYPE";
                            break;
                        case "DOC_OWNER":
                            field = "OWNER";
                            break;
                        case "DOC_IMPORTANCE":
                            field = "IMPORTANCE";
                            break;
                        case "DOC_LOCATION":
                            field = "LOCATION";
                            break;
                    }

                    System.Collections.Hashtable ht = new System.Collections.Hashtable();
                    ht[field] = value;

                    System.Collections.ArrayList alist = doc_operation.SearchDocument(ht);
                    lsv_Result.ShowData(alist, "DOC_ID", def.tb_doc);
                }
            }
        }

        private void lsv_Result_Click(object sender, EventArgs e)
        {
            if (lsv_Result.SelectedItems.Count > 0 && lsv_Result.SelectedItems[0].Tag != null)
            {
                string doc_id = lsv_Result.SelectedItems[0].Tag.ToString().Trim();

                System.Collections.Hashtable ht = doc_operation.GetDocumentInformation(doc_id);

                LoadDataFromHashToControls(splitContainer3.Panel2.Controls, ht);
            }
        }

        private void lsv_Result_DoubleClick(object sender, EventArgs e)
        {
            if (lsv_Result.SelectedItems.Count > 0 && lsv_Result.SelectedItems[0].Tag != null)
            {
                string doc_id = lsv_Result.SelectedItems[0].Tag.ToString().Trim();

                OpenDocument(doc_id);
            }
        }

        private void addDocumentToBatchProcessToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lsv_Result.SelectedItems.Count > 0 && lsv_Result.SelectedItems[0].Tag != null)
            {
                string doc_id = lsv_Result.SelectedItems[0].Tag.ToString().Trim();

                foreach (System.Windows.Forms.Form aChild in FormMain.DocLibFormMain.MdiChildren)
                {
                    if (aChild.GetType().FullName.IndexOf("FormBatchProcess")>-1)
                    {
                        //aChild.BringToFront();
                        (aChild as FormBatchProcess).AddDocumentToList(doc_id);
                        return;
                    }
                }

                //if the form does not exist
                FormBatchProcess fbp = new FormBatchProcess();
                fbp.TopLevel = true;
                fbp.MdiParent = FormMain.DocLibFormMain;
                fbp.AddDocumentToList(doc_id);
                fbp.Show();
            }
        }
    }
}